// pages/bookinfo/index.js
var CONFIG = require("../../utils/config.js")
const app = getApp()
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
      book:{},
      zhangjie:1,
      viewInfo:1,
      process:0,
      face:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
      var id=options.id
      var viewInfo=options.viewInfo
      var process = options.process
      console.log("process是"+process)
      wx.setNavigationBarTitle({
        title: app.globalData.booktitle

      });

      if(!id){
        return;
      }else{
        that.querybookById(id)
        that.setData({
          viewInfo: viewInfo,
          process:process
        })
      }
  },
  readBook:function(e){
    var that=this
    console.log(e.currentTarget.dataset.id)
    var id = e.currentTarget.dataset.id
    var content=e.currentTarget.dataset.content
    var viewInfo = that.data.viewInfo
    var process=that.data.process
    var face=that.data.face
    wx.redirectTo({
      url: '../detail/index?id=' + id + '&viewInfo='+ viewInfo+'&process='+process+'&content='+content+'&face='+face,
    })
  },
  ToZhang:function(e){
    var that=this
    var id = e.currentTarget.dataset.id
    var info = e.currentTarget.dataset.info
    // wx.navigateTo({
    //   url: '../zhangjie/index?id='+id+'&info='+info,
    // })

    setTimeout(function () {
      // wx.hideLoading();
      wx.redirectTo({
        url: '../zhangjie/index?id=' + id + '&info=' + info,
      })
    }, 380);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  getContent: function (content) {
    var that = this
    var url = CONFIG.URL.books + content//得到数据
    wx.request({
      url: url,
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'GET',
      success: function (res) {
        console.log("得到数据")
        console.log(res)
        that.setData({
          zhangjie:res.data.zhang
        })
      },
      fail: function (err) {

        console.log(err)
      }
    })
  },
  
  querybookById:function(id){
    var that=this

    var url=CONFIG.URL.querybookById
    var data={
      "id":id
    };
    CONFIG.post_from(url,data,function(res){
      console.log("200")
      console.log(res.data[0])
      if (res.statusCode == 200) {
        that.setData({
          book:res.data[0],
          face: res.data[0].nextface
        })
        var content=res.data[0].content
        app.globalData.bookinfo=res.data[0]
        that.getContent(content)
      }
    },function(err){
      console.log(err)
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  goback:function(){
    wx.navigateBack({
      delta:1
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
      wx.redirectTo({
        url: '../index/index',
      })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    return;
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})